package com.luoying.itew.util;

import android.os.Build;
import android.os.Environment;

import com.facebook.stetho.common.LogUtil;
import com.luoying.itew.common.AntaiApplication;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import okio.BufferedSink;
import okio.Okio;
import okio.Sink;

/**
 * Created by lufan on 2018/5/29
 * Class describe 文件相关的常量
 */
public class FileUtils {

    /**
     * 解压zip文件
     * @param zipFile 目标文件
     * @param targetDir 目标路径
     * @throws Exception
     */
    public static void unzip(File zipFile, File targetDir) throws Exception {
        //这里缓冲区我们使用4KB，
        int BUFFER = 4096;
        //保存每个zip的条目名称
        String strEntry;
        //文件流
        FileInputStream fis = null;
        BufferedOutputStream dest = null; //缓冲输出流
        fis = new FileInputStream(zipFile);
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry; //每个zip条目的实例
        while ((entry = zis.getNextEntry()) != null) {
            FileOutputStream fos = null;
            int count = 0;
            byte data[] = new byte[BUFFER];
            strEntry = entry.getName();
            File entryFile = new File(targetDir, strEntry);
            File entryDir = new File(entryFile.getParent());
            if (!entryDir.exists()) {
                entryDir.mkdirs();
            }
            fos = new FileOutputStream(entryFile);
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            if(fos!=null){
                fos.close();
            }
        }
        if(fis!=null){
            fis.close();
        }
    }

    /**
     * 保存文件
     * @param inputStream 输出流
     * @param locaFile 本地文件
     * @throws Exception
     */
    public static void saveFile(InputStream inputStream,File locaFile) throws Exception  {
        if(inputStream==null||!locaFile.exists()){
            throw new NullPointerException();
        }
        Sink sink;
        BufferedSink bufferedSink=null;
        sink = Okio.sink(locaFile);
        bufferedSink=Okio.buffer(sink);
        bufferedSink.writeAll(Okio.source(inputStream));
        bufferedSink.flush();
        bufferedSink.close();
    }

    /**
     * 下载文件的目录
     * @return
     */
    public static String getBasePath(){
        return  Environment.getExternalStorageDirectory() + File.separator + "AntaiDownLoad2";
//        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q){
//            return AntaiApplication.context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)+ File.separator +"AntaiDownLoad2";
//        } else {
//            return  Environment.getExternalStorageDirectory() + File.separator + "AntaiDownLoad2";
//        }
    }

    public static String upgradeFilePath(){
        String path = "";
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q){
            path = AntaiApplication.context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)+ File.separator +"upgrade"+File.separator;
        } else {
            path = Environment.getExternalStorageDirectory() + File.separator + "upgrade"+File.separator;
        }
        File file = new File(path);
        if(!file.exists()){
            file.mkdirs();
        }
        return new File(file,"antai.apk").getAbsolutePath();
    }


}
